//
//  QRCodeShowViewController.m
//  HGMMT
//
//  Created by huan liu on 2019/1/11.
//  Copyright © 2019年 eport_user1. All rights reserved.
//

#import "QRCodeShowViewController.h"
#import <CoreImage/CoreImage.h>


@interface QRCodeShowViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *showImageView;
@property (nonatomic,strong)UIImage *showImage;

@end

@implementation QRCodeShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNav];
    [self initUI];
}


#pragma mark - 生成二维码

-(void)initUI{
    
    // 1.创建过滤器 -- 苹果没有将这个字符封装成常量
     CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  
     // 2.过滤器恢复默认设置
     [filter setDefaults];
  
      // 3.给过滤器添加数据(正则表达式/帐号和密码) -- 通过KVC设置过滤器,只能设置NSData类型
      NSString *dataString = self.qrCodeContent;
      NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
      [filter setValue:data forKeyPath:@"inputMessage"];
  
      // 4.获取输出的二维码
      CIImage *outputImage = [filter outputImage];
  
      // 5.显示二维码
    UIImage *oImage = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:250];
    self.showImageView.image = oImage;
    
    //6.图片保存一份
    self.showImage = oImage;
    
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
 {
    CGRect extent = CGRectIntegral(image.extent);
        CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));

        // 1.创建bitmap;
        size_t width = CGRectGetWidth(extent) * scale;
        size_t height = CGRectGetHeight(extent) * scale;
        CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
        CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
        CIContext *context = [CIContext contextWithOptions:nil];
        CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
        CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
        CGContextScaleCTM(bitmapRef, scale, scale);
        CGContextDrawImage(bitmapRef, extent, bitmapImage);

        // 2.保存bitmap到图片
        CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
        CGContextRelease(bitmapRef);
        CGImageRelease(bitmapImage);
        return [UIImage imageWithCGImage:scaledImage];
}



#pragma mark - 初始化导航栏
-(void)initNav{
    
    self.title = @"生成的二维码";
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButton:)];
    self.navigationItem.leftBarButtonItem = left;
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButton:)];
    self.navigationItem.rightBarButtonItem = right;
    
    
}

-(void)leftButton:(UIButton *)btn
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}

-(void)rightButton:(UIButton *)btn{
    
    if (self.imageBlock) {
        NSMutableArray *mutaArray = [NSMutableArray array];
        [mutaArray addObject:self.showImage];
        self.imageBlock(mutaArray);
    }
     [self dismissViewControllerAnimated:YES completion:nil];
    
}






@end
